<?php

/**
 * Description of Account
 *
 * @author markos
 */
class VirtualLabs_Rest_VirtualLabs_Account {

    protected $_createAccountResponse;

    const TIPO_USUARIO = 0;
    const TIPO_ROOT_ADMIN = 1;
    const TIPO_DOMAIN_ADMIN = 2;

    /**
     * Crea una nueva cuenta en el cloudstack que se asocia con un area 
     * academica.
     * 
     * @param type $userModel usuario al que se asocia el account
     * @param type $usernameAccount username para el account (no debe ser el 
     * username del usuario ya que debe ser UNIQUE)
     * @param type $password password del usuario para acceso al cloudstack
     * @return type array() los datos de la cuenta creada
     */
    public function createAccount($userModel, $usernameAccount, $password) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "createAccount";
        $url["accounttype"] = self::TIPO_USUARIO; //Specify 0 for user, 1 for root admin, and 2 for domain admin
        $url["email"] = $userModel->getEmail();
        $url["firstname"] = $userModel->getNombre();
        $url["lastname"] = $userModel->getApellido();
        $url["password"] = $password;
        $url["username"] = $usernameAccount;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
        $this->_createAccountResponse = Zend_Json_Decoder::decode($response);
        $this->_createAccountResponse['register'] = $this->getUserKeys($this->_createAccountResponse['createaccountresponse']['account']['user'][0]['id']);
        return $this->_createAccountResponse;
    }
    
    /**
     * 
     * @param type $nombre
     * @return type
     */
    public function createAccount2($nombre) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "createAccount";
        $url["accounttype"] = self::TIPO_USUARIO; //Specify 0 for user, 1 for root admin, and 2 for domain admin
        $url["email"] = $nombre . "@" . $nombre . "com";
        $url["firstname"] = $nombre;
        $url["lastname"] = $nombre;
        $url["password"] = $nombre;
        $url["username"] = $nombre;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
        $this->_createAccountResponse = Zend_Json_Decoder::decode($response);
        $this->_createAccountResponse['register'] = $this->getUserKeys($this->_createAccountResponse['createaccountresponse']['account']['user'][0]['id']);
        return $this->_createAccountResponse;
    }

    /**
     * Registra las claves del usuario creado en el cloudstack y las devuelve
     * al virtual labs manager.
     * 
     * @return type array() el apiKey y secretKey para el nuevo usuario
     * cloudstack creado.
     */
    private function getUserKeys($id) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "registerUserKeys";
        $url["id"] = $id;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
        $registerResponse = Zend_Json_Decoder::decode($response);
        return $registerResponse;
    }

    /**
     * Deletes a account, and all users associated with this account.
     * 
     */
    public function deleteAccount($id) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "deleteAccount";
        $url["id"] = $id;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
        $registerResponse = Zend_Json_Decoder::decode($response);        
        return $registerResponse['jobid'];
    }

    /**
     * 
     */
    public function addAccountToProject($projectId) {
        $url = array();
        $url["response"] = "json";
        $url["command"] = "addAccountToProject";
        $url["id"] = $projectId;
        $v = new VirtualLabs_Rest_HttpClient_Client();
        $response = $v->call($url);
//        displaytext	any text associated with the success or failure
//        success	true if operation is executed successfully
        $registerResponse = Zend_Json_Decoder::decode($response);
        return $registerResponse;
    }

}
?>

